package com.demon.framework.boot.autoconfigure.swagger;


import com.demon.framework.common.model.constants.HeaderConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Swagger 相关配置
 *
 * @author Demon-HY
 * @date 2020-7-23
 */
@Configuration
@EnableSwagger2
@ConditionalOnClass(EnableSwagger2.class)
@ConditionalOnProperty(value = "swagger.enabled")
public class SwaggerAutoConfiguration {
    /**
     * 从哪个包开始扫描
     */
    @Value("${swagger.basePackage:com.demon}")
    private String SWAGGER_SCAN_BASE_PACKAGE;
    /**
     * API版本
     */
    @Value("${swagger.version:1.0.0}")
    private String VERSION;
    @Value("${swagger.title:Rest API 接口}")
    private String title;
    @Value("${swagger.description:Rest API 接口}")
    private String description;

    /**
     * 构建ApiInfo对象
     *
     * @return the api info
     */
    private ApiInfo apiInfo() {
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title(title);
        apiInfoBuilder.description(description);
        apiInfoBuilder.license("Cloud-Demon");
        apiInfoBuilder.licenseUrl("http://www.xxx.com");
        apiInfoBuilder.version(VERSION);
        apiInfoBuilder.contact(new Contact("Demon-HY",
                "https://blog.csdn.net/u014186972", "heyan_kafeibuku@sina.com"));
        return apiInfoBuilder.build();
    }

    /**
     * @return the docket
     */
    @Bean
    @ConditionalOnMissingBean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(getParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .build()
                .directModelSubstitute(LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(LocalDateTime.class, java.util.Date.class)
                .apiInfo(apiInfo());
    }

    /**
     * 获取Swagger参数
     */
    List<Parameter> getParameters() {
        Parameter auth = new ParameterBuilder()
                .name(HeaderConstants.TOKEN)
                .defaultValue("")
                .description("token 令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        Parameter clientInfo = new ParameterBuilder()
                .name(HeaderConstants.CLIENT)
                .defaultValue("{\"device\":\"web\"}")
                .description("客户端信息")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true)
                .build();

        List<Parameter> list = new ArrayList<>(4);
        list.add(auth);
        list.add(clientInfo);
        return list;
    }
}
